package com.kmvc.message.tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.kobjects.base64.Base64;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Environment;

public class ImageTools {

	public static Bitmap DrawableToBitMap(Drawable drawable) {
		Bitmap bitmap = Bitmap
				.createBitmap(
						drawable.getIntrinsicWidth(),
						drawable.getIntrinsicHeight(),
						drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
								: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
				drawable.getIntrinsicHeight());
		drawable.draw(canvas);
		drawable=null;
		return bitmap;
	}

	public static void StoreToSD(Bitmap bitmap, String PicName) {
		File fileDir = new File(Environment.getExternalStorageDirectory()
				.toString() + "/KMVC_SJJF/Pic");
		if (!fileDir.exists()) {
			fileDir.mkdirs();
		}
		File imageFile = new File(fileDir, PicName);// 目录，图片名称
		try {
			imageFile.createNewFile();
			FileOutputStream fos = new FileOutputStream(imageFile);
			bitmap.compress(CompressFormat.PNG, 50, fos);// 格式为png,jpeg背景为黑色
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		bitmap.recycle();
		bitmap=null;
	}

	/**
	 * 根据路径显示图片
	 * 
	 * @param fileName
	 * @param n
	 * @return Drawable
	 */

	public static Bitmap readBitMap(String fileName, int n) {// 根据名字读取图片
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inSampleSize = n; // width，hight设为原来的n分一
		opt.inPurgeable = true;
		opt.inInputShareable = true; // 获取资源图片
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Bitmap bitmap = BitmapFactory.decodeStream(fis, null, opt);
		// Drawable drawable = new BitmapDrawable(bitmap);
		System.gc();
		return bitmap;
	}

	/**
	 * 根据图片路径压缩图片,并转为Base64string
	 * 
	 * @param picPath
	 *            图片路径
	 * @return
	 * @throws IOException
	 */
	public String imageToBase64String(String picPath) throws IOException {

		Bitmap bitmap = readBitMap(picPath, 1);// 读出图片
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
		int options = 90;
		while (baos.toByteArray().length / 1024 > 500)// 图片大于500KB
		{
			baos.reset();// 重置baos
			bitmap.compress(CompressFormat.JPEG, options, baos);// 这里压缩为options%
			options -= 10;// 每次减少10
		}
		// 进行Base64编码
		String base64String = new String(Base64.encode(baos.toByteArray()));
		// fis.close();
		return base64String;
	}

}
